﻿// -------------------------------------------------------------------------
//    @FileName         :    NFOperatingStatistcsMgr.h
//    @Author           :    gaoyi
//    @Date             :    24-7-30
//    @Email            :    445267987@qq.com
//    @Module           :    NFOperatingStatistcsMgr
//
// -------------------------------------------------------------------------

#pragma once

#include "NFComm/NFCore/NFSingleton.hpp"
#include "proto_cs_cmd.pb.h"
#include "NFComm/NFShmStl/NFShmString.h"
#include "NFComm/NFShmStl/NFShmVector.h"
#include "proto_common_others.nanopb.h"

const int MAX_STATISTIC_NAME_LEN = 40;
#define MAX_STATISTIC_LEN  1024*1024

class CSessionMsgInfoStatistic
{
public:
	CSessionMsgInfoStatistic()
	{
		m_iTime = 0;
	}

	int Initialize();

	SingleSessionStatistic* GetRoleMsgStatistic(int iMsgID, uint64_t roleID, uint64_t uin);

	int Add(int iMsgID, int minCount, int maxCount, uint64_t roleID, uint64_t uin);

	std::string ToLog();

	void ClearTime() { m_iTime = 0; }
private:
	NFShmVector<SingleMsgStatisticInfo, proto_ff::E_CS_MAX_CMD_ID_VALUE> m_astInfo;
	int m_iTime;
};

const int CS_MAX = 5000;

typedef enum enNetStatisticType
{
	ENST_NET_IN  = 0,
	ENST_NET_OUT = 1
} ENetStatisticType;

typedef struct tagMsgStatisticData
{
	NFShmString<MAX_STATISTIC_NAME_LEN> szName;
	int iMsgID;
	int iTotalCostUs;
	int iMaxCostUs;

	int iRcvByte;
	int iRcvCount;
	int iRcvFail;
	int iSendByte;
	int iSendCount;
	int iSendFail;

	int iRealSendPkg;
	int iRealSendByte;
} MSGSTATISTICDATA;

class CMsgStatistic
{
public:
	CMsgStatistic();

	virtual ~CMsgStatistic()
	{
	}

	virtual int Initialize();
	int RegCategory(int iID, const std::string& pszName);
	virtual int Add(int iID, int Bytes, int iCostUs, int iIsFail, int iInOut, int iMulticastNum);
	bool IsValidID(int iMsgID) { return (iMsgID > 0 && iMsgID < CS_MAX); }

	std::string ToLog();
private:
	NFShmVector<MSGSTATISTICDATA, CS_MAX> m_astMsg;
	NFShmVector<int, CS_MAX> m_aiMsgIdx;
};

typedef struct tagModulaStatisticData
{
	int iID;
	NFShmString<MAX_STATISTIC_NAME_LEN> szName;

	uint64_t iTotalCostTime;
	int iCount;
	int iMaxCostTIme;
} TModulaStatisticData;

const int MAX_MODULA_NUM = 200;

class CModulaStatistic
{
public:
	CModulaStatistic(): m_aiModulaIdx(MAX_MODULA_NUM, -1)
	{
	}

	virtual ~CModulaStatistic()
	{
	}

	int Initialize();
	virtual int Add(int iID, const std::string& szName, int iCostUs);
	std::string ToLog();
private:
	bool IsModuleIDValid(int iID) { return iID >= 0 && iID < MAX_MODULA_NUM; }

	NFShmVector<TModulaStatisticData, MAX_MODULA_NUM> m_astModula;
	NFShmVector<int, MAX_MODULA_NUM> m_aiModulaIdx;
};

class NFOperatingStatistcsMgr : public NFSingleton<NFOperatingStatistcsMgr>
{
public:
	NFOperatingStatistcsMgr();

	virtual ~NFOperatingStatistcsMgr();

	virtual int Initialize(int iLogInterval, bool bIsLog);

	virtual void Tick();
	virtual int DoLog();

	int StopServer();

	int AddSessionMsgStatistic(int iMsgID, int minCount, int maxCount, uint64_t roleID, uint64_t uin)
	{
		return m_sessionMsgStatistic.Add(iMsgID, minCount, maxCount, roleID, uin);
	}

	int AddMsgStat(int iMsgID, int iByte, int iCostUs, ENetStatisticType eType, int iIsFail)
	{
		return m_msgStatistic.Add(iMsgID, iByte, iCostUs, iIsFail, eType, 0);
	}

	int AddModulaStat(int iID, const std::string& szName, int iCostUs)
	{
		return m_modulaStatistic.Add(iID, szName, iCostUs);
	}
private:
	int m_iInterval;
	bool m_bIsLog;
	time_t m_tLastTick;

	CSessionMsgInfoStatistic m_sessionMsgStatistic;
	CMsgStatistic m_msgStatistic;
	CModulaStatistic m_modulaStatistic;
};
